﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using StartEAS_API.Utils;
using StartsEAS_API.BLL.Interfaces;
using StartsEAS_API.BLL.Interfaces.Student;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;

namespace StartsEAS_API.Controllers.LoginContor
{
    [Route("api/[controller]")]
    [ApiController]
    [EnableCors("any")]
    public class LoginController : ControllerBase
    {
        private readonly ILoginBLL _loginBll;
        private readonly StarsEASContext _context;
        public LoginController(StarsEASContext context, ILoginBLL loginBll) {
            _context = context;
            _loginBll = loginBll;
        }
        /// <summary>
        /// 登录接口
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="pwd">密码</param>
        /// <param name="state">登录职位 1为学生 非0为职工</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils Login(string username, string pwd, int state)
        {
            try
            {
                AppLog.WriteInfo($"{username}尝试以{state}身份登录", true);
                if (state == 1) return _loginBll.GetStudent(username, pwd);
                else return _loginBll.GetTeacher(username, pwd);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"Login服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
    }
}
